package com.example.sentinel.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.sentinel.service.IFlowRuleHelloService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author zxs
 * @since 2020/02/19
 */
@Service
@Slf4j
public class FlowRuleHelloServiceImpl implements IFlowRuleHelloService {

    @SneakyThrows
    @SentinelResource(value = "hello1",blockHandler = "blockHandler",fallback = "fallbackHandler")
    public String hello1() {
        //long sleepTime = RandomUtil.randomLong(200L);
        //log.info("sleep:--"+sleepTime);
        //TimeUnit.MILLISECONDS.sleep(sleepTime);
        int i=0;
        int j=1;

        return "hello1";
    }

    @SneakyThrows
    @SentinelResource(value = "hello2",blockHandler = "blockHandler",fallback = "fallbackHandler")
    public String hello2() {
        long sleepTime = RandomUtil.randomLong(200L);
        log.info("sleep:--"+sleepTime);
        TimeUnit.MILLISECONDS.sleep(sleepTime);
        return "hello2";
    }

    @SentinelResource(value = "hello3",blockHandler = "blockHandler",fallback = "fallbackHandler")
    public String hello3() {
        return "hello3";
    }

    public String blockHandler(BlockException e){
        e.printStackTrace();
        return "block";
    }

    public String fallbackHandler(BlockException e){
        e.printStackTrace();
        return "fallback";
    }
}
